package com.fc.mall.service.impl;

import com.fc.mall.exception.ServiceException;
import com.fc.mall.service.SmsNoticeService;
import org.apache.http.client.fluent.Request;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;


/**
 * @Author Sunny
 * Created by Sunny on 2019/7/20.
 */
@Service
public class SmsNoticeServiceImpl implements SmsNoticeService {

    @Value("${portal.baseUrl}")
    private String SEND_NOTICE_BASE_URL;

    @Override
    public void sendNotice(String noticeBody) {

        try {
            Request.Post(SEND_NOTICE_BASE_URL)
                    .addHeader("Content-Type", "application/json")
                    .bodyString(noticeBody, ContentType.APPLICATION_JSON)
                    .execute()
                    .returnContent()
                    .asString(StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
            throw new ServiceException("请求数据，网络错误!");
        }
    }
}
